//
//  DaishouView.m
//  TengyunStation
//
//  Created by Johnny on 15/10/23.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "DaishouView.h"
#import "DaishouBatchCell.h"
#import "TSHeader.h"
#import "SocketRequest.h"
#import "Daishou.h"
#import "Singleton.h"

@interface DaishouView ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
    int _currentPage;
}

@end

@implementation DaishouView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _currentPage = 1;
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;
        self.pullTableViewDelegate = self;
        self.backgroundColor = UIColorFromRGB(0xf1f1f1);
        self.daishouExpressList = [NSMutableArray arrayWithCapacity:1];
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
        headerView.backgroundColor = [UIColor clearColor];
        self.tableHeaderView = headerView;
    }
    return self;
}

- (void)setType:(NSString *)type {
    _type = type;
    [self getDaishouListSocket];
}

- (void)reloadDaishouData {
    _currentPage = 1;
    [self getDaishouListSocket];
}

- (void)reloadTalbeView {
    if (self.daishouExpressList.count <= 0) {
        if ([self.type isEqualToString:@"1"]) {
            [self makeTip:@"无待处理快件"];
        } else {
            [self makeTip:@"无已处理快件"];
        }
    }
    [self reloadData];
}

- (void)getDaishouListSocket {
    if (self.daishouExpressList.count <= 0) {
        [self makeLoading];
    }
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STRB"]];
    [mutableData appendBytes:[SocketMethod checkLegal:self.type] length:1];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    NSString *start = [NSString stringWithFormat:@"%d",_currentPage];
    [mutableData appendBytes:[SocketMethod checkLegal:start] length:16];
    [mutableData appendBytes:[@"10" UTF8String] length:16];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    [self makeNone];
    [self endRefreshPullTableView];
    if ([code isEqualToString:SuccessSocket]) {
        if (_currentPage == 1) {
            [self.daishouExpressList removeAllObjects];
        }
        NSString *content = [SocketMethod stringDecryHexData:data];
        DLog(@"content:%@",content);
        NSArray *array = [content componentsSeparatedByString:@"|"];
        int i = 0;
        for (NSString *string in array) {
            if (string.length > 0) {
                Daishou *daishou = [[Daishou alloc] initWithString:string];
                [self.daishouExpressList addObject:daishou];
                i ++;
            }
        }
        self.isShowFooterView = (i >= 10) ? YES : NO;
        [self reloadTalbeView];
    } else {
        [self makeTip:msg];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [self makeNone];
    [self endRefreshPullTableView];
    if (self.daishouExpressList.count <= 0) {
        [self makeTip:msg Title:@"重新加载" Target:self Selector:@selector(getDaishouListSocket)];
    }
    [DSToast showText:msg InView:self.superview];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.daishouExpressList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"DaishouBatchCell";
    DaishouBatchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DaishouBatchCell" owner:self options:nil] lastObject];
        
    }
    Daishou *daishou = [self.daishouExpressList objectAtIndex:indexPath.row];
    [cell reloadDaishouBatchCell:daishou];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Daishou *daishou = [self.daishouExpressList objectAtIndex:indexPath.row];
    if ([_daishouViewDelegate respondsToSelector:@selector(didFinishSelected:DaishouView:)]) {
        [_daishouViewDelegate didFinishSelected:daishou DaishouView:self];
    }
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self pullscrollViewDidEndDragging:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self pullScrollViewDidScroll:scrollView];
}

#pragma mark PullTableViewDelegate
- (void)startRefreshHeaderView {
    _currentPage = 1;
    [self getDaishouListSocket];
}

- (void)startRefreshFooterView {
    _currentPage += 10;
    [self getDaishouListSocket];
}

@end
